package com.lazycat.backend.service.impl.shop;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lazycat.backend.mapper.GoodMapper;
import com.lazycat.backend.mapper.OrderMapper;
import com.lazycat.backend.mapper.StoreGoodMapper;
import com.lazycat.backend.mapper.StoreMapper;
import com.lazycat.backend.pojo.Good;
import com.lazycat.backend.pojo.Orders;
import com.lazycat.backend.pojo.Store;
import com.lazycat.backend.pojo.StoreGood;
import com.lazycat.backend.service.shop.BuySingleGoodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

@Service
public class BuySingleGoodServiceImpl implements BuySingleGoodService {

    @Autowired
    private GoodMapper goodMapper;  // mybatis-plus

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private StoreMapper storeMapper;

    @Override
    public Map<String, Object> buy(Integer goodId, Integer buyerId, Integer addressId) {

        QueryWrapper<Good> goodQueryWrapper = new QueryWrapper<>();
        goodQueryWrapper.eq("id", goodId);
        Good good = goodMapper.selectOne(goodQueryWrapper);
        //更新买家
        good.setBuyerId(buyerId);
        goodMapper.update(good, goodQueryWrapper);

        //增加一条订单
        QueryWrapper<Store> storeQueryWrapper = new QueryWrapper<>();
        storeQueryWrapper.eq("id", good.getStoreId());
        Store store = storeMapper.selectOne(storeQueryWrapper);
        Orders newOrder = new Orders(buyerId, store.getUserId(), good.getId(), new Date().toString(), addressId);
        orderMapper.insert(newOrder);

        HashMap<String, Object> resp = new HashMap<>();
        resp.put("error_message", "success");
        return resp;

    }
}
